package Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import models.OutputJob;
import models.User;
import models.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import util.AddandSort;

import Dao.SpringDAO;
import cn.vlabs.duckling.api.umt.rmi.exception.UserExistException;
import cn.vlabs.duckling.api.umt.rmi.user.UMTUser;
import cn.vlabs.duckling.api.vmt.VMTServiceFactory;
import cn.vlabs.duckling.api.vmt.exception.EntityNotExistException;
import cn.vlabs.duckling.api.vmt.service.GroupService;


@Service
public class UserService {
	
	@Autowired
	private DucklingContext ducklingContext;
	
	@Autowired
	private SpringDAO Dao;
	
	private boolean savedcheck = false;
	
	private User currentUser;
	
	
	
	//add job result into data base
	public void addjobs(String job, String cmd,String appname, String date, String appid, String srvurl){
		Dao.savejobs(job, currentUser.getId(), cmd, appname, date, appid, srvurl);
	}
	
	//check if user already exits
	public boolean userExists(String userId) throws Exception {
		if(Dao.availID(userId)){
			return false;
		}
		return true;
	}
	
	//get list of job output url from DB
	public List<OutputJob> getJobs(){
		return Dao.outputlist(currentUser.getId());
	}
	
	public void setCurrentUser(String email) {
		currentUser = Dao.getUser(email);
	}
	
	public void setCurrentUser(User user){
		currentUser = user;
	}
	
	//get user info
	public User getCurrentUser(){
		return currentUser;
	}
	
	
	//create user in umt
	public boolean createUserInUMT(String userName, String pass) {
		userName = userName.toLowerCase();
		String serverURL = ducklingContext.getUmtServiceURL();
		cn.vlabs.duckling.api.umt.rmi.user.UserService umtUserService = new cn.vlabs.duckling.api.umt.rmi.user.UserService(
				serverURL);
		UMTUser umtUser = new UMTUser(userName, userName, userName, pass);
		try {
			umtUserService.createUser(umtUser);
		} catch (UserExistException e1) {
			return false;
		}
		return true;
	}
	
	//add user into VO
	public boolean addUserToVO(String userId) {
		try {
			VMTServiceFactory serviceFactory = new VMTServiceFactory(
					ducklingContext.getVmtKeysDirPath(), ducklingContext
							.getVmtKeyServiceURL(), ducklingContext
							.getAppCertName());
			GroupService groupService = serviceFactory.getGroupService();
			userId = userId.toLowerCase();
			groupService.addUserToGroup(ducklingContext.getDefaultVO(), "root",
					userId);
		} catch (EntityNotExistException e1) {
			return false;
		}catch(NullPointerException e){
			e.printStackTrace();
		}
		return true;
	}
	
	//add user into database
	public boolean addUser(User user) {
		try {
			return Dao.save(user) > 0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	//add srvlist into database!
	public void setSrvList(){
		String check = Dao.getcheck();
		if(check.equals("true")){
			return;
		}
		else{
			GetOpalServiceList osl = new GetOpalServiceList();
			osl.dogetlist("service url");
			
			List<service> services = osl.getServiceList();
			
			Dao.saveSRV(services);
			Dao.setcheck("true");
		}
	}
	
	//reset dao's check -- for refresh srv list
	public void resetlist(){
		Dao.setcheck("false");
	}
	
	//get srvlist from database!
	public List<service> getSrvList(){
		return Dao.getSRV();
	}
}
